using System;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.AspNetCore.Authorization;
using Volo.Abp.Data;
using Volo.Abp.Application.Dtos;
using Joy.Erp.BasicArchives.Permissions;
using Joy.Erp.BasicArchives.Core.Filters;

namespace Joy.Erp.BasicArchives.ProductionUnit
{
    [Authorize]
    public class EquipmentAppService : BasicArchivesAppService, IEquipmentAppService
    {
        protected IEquipmentRepository EquipmentRepository => LazyServiceProvider.LazyGetRequiredService<IEquipmentRepository>();
        protected EquipmentManager EquipmentManager => LazyServiceProvider.LazyGetRequiredService<EquipmentManager>();

        [Authorize(BasicArchivesPermissions.Equipment.Query)]
        public async Task<EquipmentDto> GetAsync(Guid id)
        {
            return ObjectMapper.Map<Equipment, EquipmentDto>(
                await EquipmentRepository.GetAsync(id, true)
            );
        }

        [Authorize(BasicArchivesPermissions.Equipment.Query)]
        public async Task<EquipmentDto> FindAsync(string filter)
        {
            var entity = await EquipmentRepository.FindAsync(x => x.Code == filter);
            return ObjectMapper.Map<Equipment, EquipmentDto>(entity);
        }

        [Authorize(BasicArchivesPermissions.Equipment.Query)]
        public async Task<EquipmentDto> FindBySourceKeyAsync(string sourceKey, string sourceType)
        {
            var entity = await EquipmentRepository.FindAsync(x => x.SourceKey == sourceKey && x.SourceType == sourceType);
            return ObjectMapper.Map<Equipment, EquipmentDto>(entity);
        }

        [Authorize(BasicArchivesPermissions.Equipment.Query)]
        public async Task<PagedResultDto<EquipmentDto>> GetListAsync(FilterRequestDto input)
        {
            var totalCount = await EquipmentRepository.GetCountAsync(input.Filter, true);
            var list = await EquipmentRepository.GetPagedListAsync(input.Filter, input.SkipCount, input.MaxResultCount, input.Sorting, true);

            return new PagedResultDto<EquipmentDto>(
                totalCount,
                ObjectMapper.Map<List<Equipment>, List<EquipmentDto>>(list));
        }

        [Authorize(BasicArchivesPermissions.Equipment.Query)]
        public async Task<PagedResultDto<EquipmentDto>> QueryAsync(FilterGroupRequestDto input)
        {
            Expression<Func<Equipment, bool>> expression = null;
            if (input != null && input.FilterGroup != null)
            {
                expression = FilterConverter.Convert<Equipment>(input.FilterGroup);
            }

            var totalCount = await EquipmentRepository.GetCountAsync(expression, true);
            var list = await EquipmentRepository.GetPagedListAsync(expression, input.SkipCount, input.MaxResultCount, input.Sorting, true);

            return new PagedResultDto<EquipmentDto>(
                totalCount,
                ObjectMapper.Map<List<Equipment>, List<EquipmentDto>>(list));
        }

        [Authorize(BasicArchivesPermissions.Equipment.Create)]
        public async Task<EquipmentDto> CreateAsync(EquipmentCreateDto input)
        {
            var entity = ObjectMapper.Map<EquipmentCreateDto, Equipment>(input);

            await EquipmentManager.CreateAsync(entity, true);
            return await GetAsync(entity.Id);
        }

        [Authorize(BasicArchivesPermissions.Equipment.Update)]
        public async Task<EquipmentDto> UpdateAsync(Guid id, EquipmentUpdateDto input)
        {
            var entity = await EquipmentRepository.GetAsync(id);
            entity.SetConcurrencyStampIfNotNull(input.ConcurrencyStamp);

            ObjectMapper.Map(input, entity);

            await EquipmentManager.UpdateAsync(entity, true);
            return await GetAsync(entity.Id);
        }

        [Authorize(BasicArchivesPermissions.Equipment.Delete)]
        public async Task DeleteAsync(Guid id)
        {
            await EquipmentManager.DeleteAsync(id);
        }
    }
} 